var app = getApp();
Page({
  data: {
    begdate: '',
    enddate: '',
    moreChflag: true,
    list: [],
    consuList:[],
    eqNo: '',
    showInfo: false,
    pointArr: ["", "总支路", "第一支路", "第二支路", "第三支路", "第四支路"],
    showNone: false,
    userName: ""
  },
  onLoad: function (options) {
    var userName = wx.getStorageSync("userName")
    if (!userName || userName == "" || userName == null) {
      wx.showModal({
        title: '提示',
        content: "用户未登录",
        confirmText: '去登录',
        showCancel: false,
        success: function () {
          wx.redirectTo({
            url: '../login/login',
          })
        }
      })
    } else {
      wx.showLoading({
        title: '加载中',
      })
      this.init(userName)
    }
  },
  onReady: function () {
    // 页面渲染完成
  },
  onShow: function () {
    // 页面显示

  },
  onHide: function () {
    // 页面隐藏
  },
  onUnload: function () {
    // 页面关闭
  },
  bindBegDate: function (e) {
    this.setData({ begdate: e.detail.value })
  },
  bindEndDate: function (e) {
    this.setData({ enddate: e.detail.value })
  },
  bindNoInput: function (e) {
    this.setData({
      eqNo: e.detail.value
    })
  },
  showMoreCh: function () {
    this.setData({ moreChflag: false })
  },
  closeDig () {
    this.setData({ moreChflag: true })
  },
  checkboxChange: function (e) {
    var val = e.detail.value
    var temp = this.data.list
    for (var i = 0; i < temp.length; i++) {
      temp[i].checked = false 
    }
    temp[val].checked = true
    this.setData({ eqNo: this.data.list[val].TERMINAL_ADDRESS, list: temp })
  },
  changeRadio (e) {
    var name = e.currentTarget.dataset.name;
    this.setData({ eqNo: name})
    console.log(name)
  },
  init(userName) {
    var that = this
    var params = { userName: userName}
    var encodingStr = app.ecbEncrypto(params)
    wx.request({
      url: app.url + '/xywl/appUserMeters.htm',
      data: encodingStr,
      method: 'POST',
      success: function (res) {
        var decodingStr = app.ecbDecrypto(res.data)
        var rest = JSON.parse(decodingStr)
        if (rest.res == 0) {
          var list = rest.gasMeterList
          var nlist = []
          for (var i = 0; i < list.length; i++) {
            nlist.push(list[i][0])
          }
          that.setData({ list: nlist })
        }
      },
      complete: function () {
        wx.stopPullDownRefresh()
        wx.hideLoading()
      }
    })
  },
  query () {
    var that = this
    var meterAddr = this.data.eqNo
    var bdate = this.data.begdate
    var edate = this.data.enddate
    var params = { "meterAddr": meterAddr, "bdate": bdate, "edate": edate}
    if (meterAddr == '' || meterAddr == null) {
      wx.showModal({
        title: '提示',
        content: '请输入终端编号',
        showCancel: false
      })
      return
    }
    if (bdate == '' || bdate == null) {
      wx.showModal({
        title: '提示',
        content: '请输入起始日期',
        showCancel: false
      })
      return
    }
    if (edate == '' || edate == null) {
      wx.showModal({
        title: '提示',
        content: '请输入结束日期',
        showCancel: false
      })
      return
    }
    var leftTime = new Date(bdate.replace(/-/g, "/")) - new Date(edate.replace(/-/g, "/"));
    if (leftTime > 0) {
      wx.showModal({
        title: '提示',
        content: '起始日期不能大于结束日期',
        showCancel: false
      })
      return
    }
    wx.showLoading({
      title: '加载中',
    })
    var encodingStr = app.ecbEncrypto(params)
    wx.request({
      url: app.url + '/xywl/appQueryMeter.htm',
      data: encodingStr,
      method: 'POST',
      success: function (res) {
        var decodingStr = app.ecbDecrypto(res.data)
        var rest = JSON.parse(decodingStr)
        console.log(rest)
        if (rest.res == 0) {
          that.setData({ consuList: rest.respone.consuList, showInfo: true, showNone: false})
        } 
        if (rest.res != 0 || !rest.respone || !rest.respone.consuList || rest.respone.consuList.length <= 0) {
          that.setData({showNone: true})
        }
      },
      complete: function () {
        wx.stopPullDownRefresh()
        wx.hideLoading()
      }
    })
  }
});